package com.sacfa.commons.bo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

import com.sacfa.commons.beans.Colonia;
import com.sacfa.commons.beans.Direccion;
import com.sacfa.commons.connection.PoolConexion;
import com.sacfa.commons.sql.DireccionSQL;
import com.sacfa.commons.utilities.SACFAExceptions;

/**
 * @author Leizar Zar
 * @version 1.0
 * @created 11-ago-2013
 */
public class DireccionBo {
	private static Logger log = Logger.getLogger(DireccionBo.class);
	
	public int registrar(Direccion direccion, Connection conex){
		int id = 0;
		PreparedStatement stm = null;
		ResultSet rs = null;
		
		try {
			stm = conex.prepareStatement(DireccionSQL.INSERT, Statement.RETURN_GENERATED_KEYS);
			stm.setString(1, direccion.getCalle());
			stm.setInt(2, direccion.getNumeroExterno());
			stm.setInt(3, direccion.getNumeroInterno());
			stm.setInt(4, direccion.getColonia().getId());
			stm.setInt(5, direccion.getCodigoPostal());
			stm.executeUpdate();
			
			rs = stm.getGeneratedKeys();
			rs.next();
			id = rs.getInt(1);
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		}
		return id;
	}

	public Direccion consultar(Direccion direccion){
		Colonia colonia = new Colonia();
		PreparedStatement stm = null;
		ResultSet rs = null;
		Connection conex = PoolConexion.getInstance().getConexion();
		
		try {
			stm = conex.prepareStatement(DireccionSQL.SELECT_ONE);
			stm.setInt(1, direccion.getId());
			rs = stm.executeQuery();
			
			rs.next();
			direccion.setId(rs.getInt(1));
			direccion.setCalle(rs.getString(2));
			direccion.setNumeroExterno(rs.getInt(3));
			direccion.setNumeroInterno(rs.getInt(4));
			colonia.setId(rs.getInt(5));
			direccion.setColonia(colonia);
			direccion.setCodigoPostal(rs.getInt(6));
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		} finally {
			PoolConexion.getInstance().endConection(conex);
		}
		return direccion;
	}
	
	public void modificar(Direccion direccion){
		PreparedStatement stm = null;
		Connection conex = PoolConexion.getInstance().getConexion();
		
		try {
			PoolConexion.getInstance().beginTransaction(conex);
			
			stm = conex.prepareStatement(DireccionSQL.UPDATE);
			stm.setString(1, direccion.getCalle());
			stm.setInt(2, direccion.getNumeroExterno());
			stm.setInt(3, direccion.getNumeroInterno());
			stm.setInt(4, direccion.getColonia().getId());
			stm.setInt(5, direccion.getCodigoPostal());
			stm.executeUpdate();
			
			PoolConexion.getInstance().commit(conex);
			PoolConexion.getInstance().endTransaction(conex);
		} catch(SQLException ex) {
			log.info("Error SQL", ex);
			throw new SACFAExceptions("Error SQL", ex);
		} finally {
			PoolConexion.getInstance().endConection(conex);
		}
	}
}
